package com.syni.mdd.yhd.common.all.service.impl.platformGood;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.BeanUtils;
import com.syni.mdd.sjb.common.component.utils.common.utils.ParamsMap;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsBusinessCouponDao;
import com.syni.mdd.yhd.common.all.dao.jpa.platformGood.BmsPlatformGoodDao;
import com.syni.mdd.yhd.common.all.dto.requestDto.business.BusinessCouponReDto;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.BmsBusinessCoupon;
import com.syni.mdd.yhd.common.all.entity.BmsDxGroupBuy;
import com.syni.mdd.yhd.common.all.entity.BmsDxSubFood;
import com.syni.mdd.yhd.common.all.entity.coupon.BmsUserReceCoupon;
import com.syni.mdd.yhd.common.all.entity.platformGood.BmsPlatformGood;
import com.syni.mdd.yhd.common.all.entity.platformGood.PlatformGoodDto;
import com.syni.mdd.yhd.common.all.entity.platformGood.vo.PlatformGoodVo;
import com.syni.mdd.yhd.common.all.enums.platformGood.PlatformGoodEnum;
import com.syni.mdd.yhd.common.all.quartz.QuartzDo;
import com.syni.mdd.yhd.common.all.quartz.businessCoupon.job.CouponAsOfJob;
import com.syni.mdd.yhd.common.all.quartz.businessCoupon.job.CouponToSendJob;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.yhd.common.all.service.impl.BaseService;
import com.syni.mdd.yhd.common.all.service.platformGoods.BmsPlatformGoodService;
import com.syni.mdd.yhd.common.all.utils.*;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @className
 * @description TOO
 * @Author hwq
 * @DATE 2019/9/23 10:34
 * @VERSION 1.0
 **/
@Service
public class BmsPlatformGoodServiceImpl extends BaseService<BmsPlatformGood, Integer> implements BmsPlatformGoodService {


    @Autowired
    BmsPlatformGoodDao bmsPlatformGoodDao;

    @Autowired
    RedisTemplate redisTemplate;

    @Autowired
    BmsBusinessService bmsBusinessService;

    @Override
    public BmsPlatformGoodDao getBaseDao() {
        return bmsPlatformGoodDao;
    }

    @Override
    public Map<String, Object> getMasksGood(ParamsMap paramsMap) {
        Integer buyArea = paramsMap.getInteger("buyArea");
        Optional<BmsPlatformGood> bmsPlatformGoodOptional = bmsPlatformGoodDao.findById(MASK_ID);
        if(!bmsPlatformGoodOptional.isPresent()){
            return ResultMapHelper.result(PlatformGoodEnum.PLATFORM_GOOD_NO_EXIST);
        }
        BmsPlatformGood bmsPlatformGood = bmsPlatformGoodOptional.get();
        PlatformGoodDto platformGoodDto = new PlatformGoodDto();
        BeanUtils.copyProperties(bmsPlatformGood,platformGoodDto);
        //处理部分逻辑
        BmsBusiness bmsBusiness = bmsBusinessService.getBusinessById(platformGoodDto.getBusinessId());
        platformGoodDto.setBmsBusiness(bmsBusiness);
        PlatformGoodVo platformGoodVo = new PlatformGoodVo();
        BeanUtils.copyProperties(platformGoodDto,platformGoodVo);
        platformGoodVo.setGoodId(platformGoodDto.getId());
        if(buyArea == BUY_AREA_OUT){
            platformGoodVo.setGoodPrice(platformGoodDto.getGoodPriceOut());
        }
        if(bmsBusiness != null) {
            platformGoodVo.getBusinessVo().setVendorName(bmsBusiness.getVendorName());
            platformGoodVo.getBusinessVo().setBusinessId(bmsBusiness.getId());
        }
        return ResultMapHelper.success(platformGoodVo,ResultMsg.MSG_GET_SUCCESS);
    }

    public BmsPlatformGood findById(Integer goodId){
        Optional<BmsPlatformGood> bmsPlatformGoodOptional = bmsPlatformGoodDao.findById(goodId);
        if(!bmsPlatformGoodOptional.isPresent()){
            return null;
        }
        return bmsPlatformGoodOptional.get();
    }

    @Override
    public Integer updateInventoryNum(Integer id,Integer num,Integer version) {
        return bmsPlatformGoodDao.updateInventoryNum(id,num,version);
    }

    @Override
    public Integer updateLoseGoodInventory(Integer id, Integer num, Integer version) {
        return bmsPlatformGoodDao.updateLoseGoodInventory(id,num,version);
    }

    @Override
    public Map<Integer, BmsPlatformGood> findByIdIn(Set<Integer> platformIds) {
        List<BmsPlatformGood> bmsPlatformGoods = bmsPlatformGoodDao.findByIdIn(platformIds);
        Map<Integer,BmsPlatformGood> bmsPlatformGoodMap = new HashMap<>();
        if(bmsPlatformGoods != null && bmsPlatformGoods.size() > 0){
            for(BmsPlatformGood bmsPlatformGood : bmsPlatformGoods){
                bmsPlatformGoodMap.put(bmsPlatformGood.getId(),bmsPlatformGood);
            }
        }
        return bmsPlatformGoodMap;
    }
}
